package model.dao;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;

import utils.Constants;

public class DataSourceFactory {

	private static Logger logger = Logger.getLogger(DataSourceFactory.class);
	private static DriverManagerDataSource dataSource = null;
	private static boolean debug = false;

	private DataSourceFactory() {
	}

	public static synchronized DataSource getDataSource() {
		if (dataSource == null) {
			logger.debug("Creando data source");
			dataSource = new SingleConnectionDataSource();
			dataSource.setDriverClassName("org.sqlite.JDBC");
			String base = "";

			if (debug)
				base = System.getProperty("user.dir") + "/data/base/base.sqlite";
			else
				base = Constants.SYSTEM_DIR + "/data/base/base.sqlite";

			dataSource.setUrl("jdbc:sqlite:" + base);
		}
		return dataSource;

	}

	public static void setDebug(boolean flag) {
		debug = flag;
	}

	public static boolean isDebug() {
		return debug;
	}
}